package com.blue.facade.sys.user.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.blue.common.support.BaseService;
import com.blue.facade.sys.user.entity.Menu;
import com.blue.facade.sys.user.mapper.MenuMapper;
import com.blue.facade.sys.user.service.MenuFacade;

@Service
public class MenuFacadeImpl extends BaseService<Menu, Long> implements MenuFacade {
	
	@Autowired
	private MenuMapper mapper ;

	@Override
	public List<Menu> getMenuCategory() {
		return this.mapper.getMenuCategory();
	}

	@Override
	public List<Menu> getMenuByParentId(Long id) {
		List<Menu> list = new ArrayList<Menu>() ;
		List<Menu> menuByCategory = this.mapper.getMenuByParentId(id) ;
		for (Menu menu : menuByCategory) {
			list.add(recursiveMenu(menu)) ;
		}
		return list ;
	}
	
	@Override
	public List<Menu> getAllMenus() {
		List<Menu> list = new ArrayList<Menu>() ;
		List<Menu> menuCategory = this.mapper.getMenuCategory() ;
		for (Menu menu : menuCategory) {
			list.add(recursiveMenu(menu)) ;
		}
		return list;
	}
	
	public Menu recursiveMenu(Menu menu) {
		List<Menu> childs = this.mapper.getMenuByParentId(menu.getId()) ;
		if(null != childs && childs.size() > 0) {
			List<Menu> children = new ArrayList<Menu>() ;
			
			for (Menu m : childs) {
				children.add(recursiveMenu(m)) ;
			}
			menu.setChildren(children);
		}
		return menu ;
	}



}
